package com.shop2cn.iapi.prodstockmgr.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.shop2cn.iapi.prodstockmgr.parameter.AddImportTaskBean;
import com.shop2cn.iapi.prodstockmgr.service.AddImportTaskCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.GetProductListBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.shop2cn.iapi.agentproducts.parameter.ProductListDto;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.Shop2cnStockCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.stockmgr.agentinventory;
import com.ymttest.database.model.stockmgr.agentinventoryExample;
import com.ymttest.database.model.stockmgr.agentinventorysku;
import com.ymttest.database.model.stockmgr.agentinventoryskuExample;
import com.ymttest.database.sqlwapper.stockmgr.agentinventoryWapper;
import com.ymttest.database.sqlwapper.stockmgr.agentinventoryskuWapper;
import com.ymttest.utils.ExcleUtil;
import com.ymttest.utils.StockExcleUtil;

/**
 * 创建导入任务Test FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_AddImportTask {
	private static AddImportTaskBean addimporttaskBean;
	private static AddImportTaskCall addimporttaskCall;
	
	agentinventoryExample inventoryExample=new agentinventoryExample();
	agentinventoryWapper inventory=new agentinventoryWapper();
	
	agentinventoryskuExample skuExample=new agentinventoryskuExample();
	agentinventoryskuWapper sku=new agentinventoryskuWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建导入任务Test");
	}

	@Before
	public void caseUp() {
		addimporttaskBean = new AddImportTaskBean();
		addimporttaskCall = new AddImportTaskCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(addimporttaskCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AddImportTask_001() {
		Logger.start(true, "导入十件库存商品，成功");
		try {
			Integer mchid = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchid,
					"", true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			addAgentProductBean.getCatalogList().get(0).setSku("112233");
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			Logger.comment("入库前的数量");
			inventoryExample.createCriteria().andSellerIdEqualTo(mchid);	
			List<agentinventory> inventorys1=inventory.selectByExample(inventoryExample);
			
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(mchid, productId);
			
			String uploadOne = StockExcleUtil.uploadOne(addAgentProductBean.getCatalogList().get(0).getSku(), 10, 60);
			addimporttaskBean.setFileUrl(uploadOne);
			addimporttaskBean.setSellerId(mchid);
			addimporttaskBean.setFileName("aa");
			addimporttaskCall.setData(addimporttaskBean);
			addimporttaskCall.callService();
			Logger.verifyEquals("200", addimporttaskCall.getString("code"), "验证code");
			Shop2cnStockCallService.Execute();
			Thread.sleep(5000);
			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(mchid, productId);
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getStock()+10,productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getRealStock()+10,productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
			
			inventoryExample.createCriteria().andSellerIdEqualTo(mchid);
			inventoryExample.setOrderByClause(" id desc ");
			skuExample.createCriteria().andProductIdEqualTo(productId).andCatalogIdEqualTo(productDetailsDto.getCatalogList().get(0).getCatalogId());
			skuExample.setOrderByClause(" id desc ");
			List<agentinventorysku> skus=sku.selectByExample(skuExample);
			Logger.comment("入库后的数量");
			List<agentinventory> inventorys2=inventory.selectByExample(inventoryExample);
			Logger.verifyEquals(2, skus.size(), "验证agentinventorysku中新增一条数据");
			Logger.verifyEquals(2, inventorys2.size()-inventorys1.size(), "验证agentinventory中新增一条数据");
			
			agentinventorysku sku=skus.get(0);
			agentinventory inventory=inventorys2.get(0); 
			Logger.verifyEquals(10,sku.getNum(), "验证agentinventorysku中的库存");
			Logger.verifyEquals(1,inventory.getType(), "验证agentinventorysku中的type=1手动入库");
			Logger.verifyNotNull(inventory.getInventoryNo(), "验证agentinventory中的InventoryNo单号");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}